/**
 * User: alesj
 * Date: 2004.9.7
 * Time: 12:24:22
 */

package com.generalynx.ecos.web.form;

import com.generalynx.common.data.IEntity;
import com.generalynx.common.resources.Resources;
import com.generalynx.ecos.data.Role;
import com.generalynx.ecos.data.User;
import com.generalynx.ecos.data.UserRole;

import java.util.Set;

public class RoleSelectForm extends AbstractSingleSelectForm {

    protected String getHeaderLabel() {
        return Resources.getString(Resources.ROLENAME);
    }

    protected String getBodyLabel() {
        return Resources.getString(Resources.USERS);
    }

    protected Class getEntityClass() {
        return Role.class;
    }

    protected Class getMembersClass() {
        return User.class;
    }

    protected Set getMembers(IEntity entity) {
        return ((Role)entity).getUsers();
    }

    protected void doLeft(int[] left, Set members, IEntity entity) {
        Role role = (Role)entity;
        for (int i = 0; i < left.length; i++) {
            User user = getBasicDAO().loadUser(left[i]);
            UserRole ur = new UserRole();
            ur.setUserId(user.getId());
            ur.setUsername(user.getUsername());
            ur.setRoleId(role.getId());
            ur.setRolename(role.getRolename());
            getBasicDAO().saveUserRole(ur);
        }
    }
}
